# -*- coding: utf-8 -*-
import os
import sys

from loguru import logger

from config.config import config


def getLogger(name: str = "default"):
    instances = {}

    def _singleton():
        if name not in instances:
            logPath = config.get("logger", "log_path")
            format = config.get("logger", "format")
            logger.remove()
            logger.add(sys.stderr, level="INFO", format=format, enqueue=True)
            logger.add(os.path.join(logPath, 'runtime_{time}.log'), level="INFO", format=format, rotation="1 day",
                       retention='3 days', compression='zip', enqueue=True)
            logger.add(os.path.join(logPath, 'ssuccess_{time}.log'), level="SUCCESS", format=format, rotation="1 day",
                       retention='3 days', compression='zip', enqueue=True)
            logger.add(os.path.join(logPath, 'error_{time}.log'), level="ERROR", format=format, rotation="1 day",
                       retention='3 days', compression='zip', enqueue=True)
            instances[name] = logger
        return instances[name]
    return _singleton()
